package cn.iocoder.yudao.module.book.dal.mysql.audio;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.controller.app.eargrinding.vo.AppAudioRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.audio.AudioDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.book.controller.admin.audio.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 音频 Mapper
 *
 * @author xi
 */
@Mapper
public interface AudioMapper extends BaseMapperX<AudioDO> {

    default PageResult<AudioDO> selectPage(AudioPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AudioDO>()
                .eqIfPresent(AudioDO::getAudioId, reqVO.getAudioId())
                .eqIfPresent(AudioDO::getUserId, reqVO.getUserId())
                .likeIfPresent(AudioDO::getAudioUrl, reqVO.getAudioUrl())
                .eqIfPresent(AudioDO::getBookId, reqVO.getBookId())
                .eqIfPresent(AudioDO::getProgressSeconds, reqVO.getProgressSeconds())
                .betweenIfPresent(AudioDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AudioDO::getAudioId));
    }

    List<AppAudioRespVO> getByUserId(@Param("userId") Integer userId,@Param("sort") String sort);

    AppAudioRespVO getAudioByBookId(@Param("bookId") Integer bookId);

    List<AppAudioRespVO> getRecentAudio(@Param("userId") Long userId,@Param("sevenDaysAgo") LocalDateTime sevenDaysAgo);

    void insertRecord(AudioDO audio);

    AudioDO selectLatestByUserBookPage(@Param("userId") Long userId,
                                       @Param("bookId") Long bookId,
                                       @Param("page") Integer page);

    int updateDataByUserBookPage(@Param("userId") Long userId,
                                  @Param("bookId") Long bookId,
                                  @Param("page") Integer page,
                                  @Param("audioData") byte[] audioData,
                                  @Param("contentType") String contentType);

    int updateUrlByUserBookPage(@Param("userId") Long userId,
                                 @Param("bookId") Long bookId,
                                 @Param("page") Integer page,
                                 @Param("audioUrl") String audioUrl);

    /**
     * 查询指定用户录音过的书本ID（去重）
     */
    List<Long> selectDistinctBookIdsByUser(@Param("userId") Long userId);
}